/**
 * .
 * Copyright (c) 2013-2018 All Rights Reserved.
 */
package com.lcy.demotest.service;

import com.lcy.demotest.dao.UserMapper;
import com.lcy.demotest.exception.BaseRuntimeException;
import com.lcy.demotest.model.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author playersix
 * @version $Id: UserService.java, v 0.1 2018-10-09 15:24 playersix Exp $$
 */
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public User add(User u) {
        if (null == u || StringUtils.isBlank(u.getName())) {
            throw new BaseRuntimeException("user cannot null");
        }
        userMapper.insertSelective(u);
        return u;
    }

    public User query(Long id) {
        if (null == id) {
            throw new BaseRuntimeException("id cannot null");
        }
        return userMapper.selectByPrimaryKey(id);
    }

    public int update(User u) {
        if (null == u || StringUtils.isBlank(u.getName())) {
            throw new BaseRuntimeException("user cannot null");
        }
        if (null == u.getId()) {
            throw new BaseRuntimeException("id cannot null");
        }
        return userMapper.updateByPrimaryKeySelective(u);
    }

    public int delete(Long id) {
        if (null == id) {
            throw new BaseRuntimeException("id cannot null");
        }
        return userMapper.deleteByPrimaryKey(id);
    }

}
